package com.cn.intelligence.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cn.intelligence.mapper.MemberMapper;
import com.cn.intelligence.pojo.Member;
import com.cn.intelligence.pojo.MemberExample;
import com.cn.intelligence.pojo.TaotaoResult;
import com.cn.intelligence.service.MemberService;
import com.cn.intelligence.util.IDUtils;

/**
 * Created by Administrator on 2017/4/18.
 */
@Service
public class MemberServiceImpl implements MemberService {
    @Autowired
    private  MemberMapper memberMapper;
    @Override
    public List<Map<String,Object>> getItemCatList(long parentId) {
        // 查询类目标
        MemberExample memberExample = new MemberExample();
        MemberExample.Criteria criteria=memberExample.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<Member>  memberList= memberMapper.selectByExample(memberExample);
        List<Map<String,Object>> treenodelist = new ArrayList<Map<String,Object>>();
        for (Member member:memberList) {
        	Map<String,Object> node = new HashMap<String,Object>();
        	node.put("id", member.getId());
        	node.put("text", member.getMembername());
        	node.put("memberno", member.getMemberno());
        	node.put("isParent", member.getIsParent());
        	node.put("state",member.getIsParent()?"closed":"open");
        	node.put("tel",member.getTel());
        	node.put("job",member.getJob());
        	node.put("sex",member.getSex());
        	node.put("provice",member.getProvice());
        	node.put("city",member.getCity());
        	node.put("vipcode", member.getVipcode());
        	node.put("level", member.getLevel());
        	node.put("created", member.getCreated());
        	node.put("updated", member.getUpdated());
        	treenodelist.add(node);
      }
        return treenodelist;
    }
	@Override
	public TaotaoResult addMember(Member member) {
		Long id = IDUtils.genItemId();
		member.setId(id);
		member.setIsParent(member.getParentId()==null?true:false);
		member.setParentId(member.getParentId()==null?0l:member.getParentId());
		member.setCreated(new Date());
		member.setUpdated(new Date());
		int insertvalue = memberMapper.insert(member);
		if(insertvalue == 1){
			return TaotaoResult.ok(member);
		}else{
			return TaotaoResult.fail();
		}
	}
	@Override
	public TaotaoResult updateItemCat(Member Member) {
        MemberExample MemberExample = new MemberExample();
        MemberExample.Criteria criteria=MemberExample.createCriteria();
        criteria.andIdEqualTo(Member.getId());
		Member.setUpdated(new Date());
		int update = memberMapper.updateByExampleSelective(Member, MemberExample);
		if(update == 1){
			return TaotaoResult.ok(Member);
		}else{
			return TaotaoResult.fail();
		}
	}
	@Override
	public TaotaoResult deleteItemCat(Long id) {
		int flag = memberMapper.deleteByPrimaryKey(id);
		if(flag == 1){
			return TaotaoResult.ok();
		}else{
			return TaotaoResult.fail();
		}        
	}

	
	@Override
	public TaotaoResult getItemCatDesc(String catid) {
		Member cat = memberMapper.selectByPrimaryKey(Long.parseLong(catid));
		return TaotaoResult.ok(cat);
	}
	@Override
	public TaotaoResult jiesuan(Long id) {
        MemberExample MemberExample = new MemberExample();
        MemberExample.Criteria criteria=MemberExample.createCriteria();
        criteria.andIdEqualTo(id);
        Member member = new Member();
        member.setUpdated(new Date());
		int update = memberMapper.updateByExampleSelective(member, MemberExample);
		
        MemberExample MemberExample2 = new MemberExample();
        MemberExample.Criteria criteria2=MemberExample2.createCriteria();
        criteria2.andParentIdEqualTo(id);
        Member member2 = new Member();
        member2.setUpdated(new Date());
		int update2 = memberMapper.updateByExampleSelective(member2, MemberExample2);
		
		if(update + update2>=1){
			return TaotaoResult.ok(member);
		}else{
			return TaotaoResult.fail();
		}
	}
}
